<?php

namespace App\Http\Controllers;

use App\Student;
use Dotenv\Validator;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Session;

class StudentController extends Controller
{
    public function test1()
    {
        $num = DB::update('update llc_student set age = ? where name = ?',
            [38, 'tomas']
        );
        var_dump($num);

        $students = DB::select('select * from llc_student');
        var_dump($students);
//        $bool = DB::insert('insert into llc_student values(?,?,?)', [2, 'tomas', 37]);
//        var_dump($bool);

        $num = DB::delete('delete from llc_student where name = ?',
            ['tomas']
        );
        var_dump($num);
    }

    public function test2()
    {
        $id = DB::table('llc_student')->insertGetId(['id' => 3, 'name' => 'emily', 'age' => 40]);
        var_dump($id);
    }

    public function test3()
    {
        $num = DB::table('llc_student')->where('id', 3)
            ->decrement('age', 1, ['name' => 'Emily']);
        var_dump($num);
    }

    public function test4()
    {
        $student = DB::table('llc_student')
            ->select('name', 'age')
            ->whereRaw('id >= ? and name = ?', [2, 'tomas'])
            ->orderBy('name', 'desc')
            ->get();
        $ages = DB::table('llc_student')
            ->pluck('age');

        echo '<pre>';
        DB::table('llc_student')->orderBy('name', 'desc')->chunk(2, function ($students) {
            var_dump($students);
            return false;
        });
    }

    public function test5()
    {
        $num = DB::table('llc_student')->min('age');
        var_dump($num);
    }

    public function orm1()
    {
        $student = Student::find(1);
        dd($student);
    }

    public function orm2()
    {
//        $student = new Student();
//        $student->id = 5;
//        $student->name = 'sean2';
//        $student->age = 10;
//        $bool = $student->save();
//        $student = Student::create(
//            ['id' => 6, 'name' => 'sean3', 'age' => 12]
//        );

        $student = Student::firstOrCreate(
            ['id' => 7, 'name' => 'imooc', 'age' => 20]
        );
        dd($student);
    }

    public function orm3()
    {
//        $student = Student::find(7);
//        $student->name = 'kitty';
//        $bool = $student->save();
        $num = Student::where('id', '>', 5)->update(
            ['age' => 40]
        );
        var_dump($num);
    }

    public function orm4()
    {
//        $student = Student::find(7);
//        $bool = $student->delete();
//        $num = Student::destroy(4, 5);
        $num = Student::where('id', '>', 2)->delete();
        var_dump($num);
    }

    public function section1()
    {
        $name = 'sean';
        $arr = ['bill', 'sean'];
//        $students = Student::get();
        $students = [];
        return view('student.section',
            ['name' => $name, 'arr' => $arr, 'students' => $students]
        );
    }

    public function urlTest()
    {
        return 'urlTest';
    }

    public function request1(Request $request)
    {
//        echo $request->input('sex','未知');
//
//        if ($request->has('name'))
//        {
//            echo $request->input('name');
//        } else
//        {
//            echo 'no this parameter.';
//        }
//
//        $res = $request->all();
//        dd($res);
        $res = $request->url();
        var_dump($res);

    }

    public function session1(Request $request)
    {
//        $request->session()->put('key1', 'value1');
//        echo $request->session()->get('key1');
//        session()->put('key2', 'value2');
//        echo session()->get('key3','default value');
//        Session::put('key3', 'value3');
//        echo Session::get('key3');
//        Session::put(['key4' => 'value4']);
//        echo Session::get('key4');
//        Session::push('student', 'sean');
//        Session::push('student', 'bill');
//        $res = Session::get('student', 'default value');
//        var_dump($res);
//        $res = Session::pull('student', 'default value');
//        var_dump($res);
//        if (Session::has('key5')) {
//            $res = Session::all();
//            dd($res);
//        } else
//        {
//            echo 'The key5 is not in Session.';
//        }
//        Session::flush();
//        $res = Session::all();
//        dd($res);
        Session::flash('key-flash', 'value-flash');
    }

    public function session2(Request $request)
    {
        echo Session::get('key-flash', '默认数据');
    }

    public function response()
    {
//        $data = [
//            'errCode' => 0,
//            'errMsg' => 'success',
//            'data' => 'sean',
//        ];
//        return response()->json($data);

//        return redirect('session2')->with('key-flash','我是快闪数据');
//        return redirect()->action('StudentController@session2')
//            ->with('key-flash','我是快闪数据2');
//        return redirect()->route('session21')->with('key-flash','我是快闪数据3');
        return redirect()->back();
    }

    public function activity0()
    {
        return '活动即将开始.';
    }

    public function activity1()
    {
        return '活动1进行中.';
    }

    public function activity2()
    {
        return '活动2进行中.';
    }


    // 学生列表页
    public function index()
    {
        $students = Student::paginate(20);
        return view("student.index", ['students' => $students,]);
    }

    // 新增学生页
    public function create(Request $request)
    {
        if ($request->isMethod("POST")) {
            $validator = \Validator::make($request->input(), [
                'Student.name' => 'required|min:2|max:20',
                'Student.age' => 'required|integer',
            ], [
                'required' => ':attribute 为必填项',
                'min' => ':attribute 不符合最小长度',
                'max' => ':attribute 不符合最大长度',
                'integer' => ':attribute 必须为整数',
            ], [
                'Student.name' => '姓名',
                'Student.age' => '年龄',
            ]);

            if ($validator->fails()) {
                return redirect()->back()->withErrors($validator)->withInput();

            }
            $data = $request->input('Student');
            $data['created_at'] = date('Y-m-d H:i:s', time());
            $data['updated_at'] = date('Y-m-d H:i:s', time());
            var_dump($data);
            $data['id'] = 13;
            if (Student::create($data)) {
                return redirect('student/index')->with("success", "添加成功！");
            } else {
                return redirect()->back();
            }
        }
        $student = new Student();
        return view("student.create", ['student' => $student]);
    }

    // 保存学生页
    public function save(Request $request)
    {

        /*
        $this->validate($request, [
            'Student.name' => 'required|min:2|max:20',
            'Student.age' => 'required|integer',
        ], [
            'required' => ':attribute 为必填项',
            'min' => ':attribute 不符合最小长度',
            'max' => ':attribute 不符合最大长度',
            'integer' => ':attribute 必须为整数',
        ], [
            'Student.name' => '姓名',
            'Student.age' => '年龄',
        ]);
        */

        // validator($request);
        $validator = \Validator::make($request->input(), [
            'Student.name' => 'required|min:2|max:20',
            'Student.age' => 'required|integer',
        ], [
            'required' => ':attribute 为必填项',
            'min' => ':attribute 不符合最小长度',
            'max' => ':attribute 不符合最大长度',
            'integer' => ':attribute 必须为整数',
        ], [
            'Student.name' => '姓名',
            'Student.age' => '年龄',
        ]);

        if ($validator->fails()) {
            return redirect()->back()->withErrors($validator)->withInput();

        }
        $data = $request->input('Student');
        $student = new Student();
        $student->id = 9;
        $student->name = $data['name'];
        $student->age = $data['age'];
        $student->created_at = date('Y-m-d H:i:s', time());
        $student->updated_at = date('Y-m-d H:i:s', time());
        if ($student->save()) {
            return redirect('student/index')->with("success", "添加成功！");
        } else {
            return redirect()->back();
        }
    }

    public function update(Request $request, $id)
    {
        $student = Student::find($id);
        if ($request->isMethod("POST")) {
            $validator = \Validator::make($request->input(), [
                'Student.name' => 'required|min:2|max:20',
                'Student.age' => 'required|integer',
            ], [
                'required' => ':attribute 为必填项',
                'min' => ':attribute 不符合最小长度',
                'max' => ':attribute 不符合最大长度',
                'integer' => ':attribute 必须为整数',
            ], [
                'Student.name' => '姓名',
                'Student.age' => '年龄',
            ]);

            if ($validator->fails()) {
                return redirect()->back()->withErrors($validator)->withInput();

            }
            $data = $request->input('Student');
            $student->name = $data['name'];
            $student->age = $data['age'];
            if ($student->save()) {
                return redirect('student/index')->with("success", "修改成功-" . $id);
            } else {
                return redirect()->back();
            }
        }

        return view('student.update', ['student' => $student]);
    }

    public function validator(Request $request)
    {
        $validator = \Validator::make($request->input(), [
            'Student.name' => 'required|min:2|max:20',
            'Student.age' => 'required|integer',
        ], [
            'required' => ':attribute 为必填项',
            'min' => ':attribute 不符合最小长度',
            'max' => ':attribute 不符合最大长度',
            'integer' => ':attribute 必须为整数',
        ], [
            'Student.name' => '姓名',
            'Student.age' => '年龄',
        ]);

        if ($validator->fails()) {
            return redirect()->back()->withErrors($validator)->withInput();
        }
    }

    public function detail($id)
    {
        $student = Student::find($id);
        return view('student.detail', ['student' => $student]);
    }

    public function delete($id)
    {
        $student = Student::find($id);
        if ($student->delete()) {
            return redirect('student/index')->with("success", "删除成功-" . $id);
        } else {
            return redirect('student/index')->with("error", "删除失败-" . $id);
        }
    }
}